package nl.haemers.data.reflection 
{
	import nl.haemers.data.constants.ReflectionConstants;
	import nl.haemers.data.reflection.IMetadatas;
	import nl.haemers.data.reflection.IParameters;
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	public class Method implements IMethod 
	{
		private var _name				:String;
		private var _methodName			:String;
		private var _declaredBy			:String;
		private var _returnType			:String;
		
		private var _parameters			:IParameters;
		private var _metadatas			:IMetadatas;
		
		private var _level				:int;
		
		private var _hasSerialize		:Boolean;
		private var _objType			:Type;
		
		private var _ignore				:Boolean;
		
		public function Method(description:XML) 
		{
			init(description);
		}
		
		/** Public methods **/
		public function hasSerialize() : Boolean 
		{
			return _hasSerialize;
		}
		
		public function toString() : String 
		{
			return _name;
		}
		
		/** Getters & Setters **/
		public function get name() : String 
		{
			return _name;
		}
		
		public function get methodName() : String
		{
			return _methodName;
		}
		
		public function get declaredBy() : String 
		{
			return _declaredBy;
		}
		
		public function get returnType() : String 
		{
			return _returnType;
		}
		
		public function get parameters() : IParameters 
		{
			return _parameters;
		}
		
		public function get metadatas() : IMetadatas 
		{
			return _metadatas;
		}
		
		public function get ignore() : Boolean
		{
			return _ignore;
		}
		
		/** Static methods **/
		private function init(xml:XML) : Boolean
		{
			//var method:Method = new Method();
			var paramNames:Array;
			
			_metadatas = new Metadatas(xml);
			var serialize:IMetadata = _metadatas.getMetadata(ReflectionConstants.OBJECT_ATTRIBUTE);
			_hasSerialize = serialize != null;
			
			this._name = _methodName = xml.@name;
			if (_hasSerialize)
			{
				paramNames = getParamNames(serialize);
				
				//TODO: Maybe we need to convert this to a ObjectType not sure yet.
				if (serialize.args.hasArg(ReflectionConstants.ATTRIBUTE_NAME))
					this._name = serialize.args.getArg(ReflectionConstants.ATTRIBUTE_NAME).value;
					
				if (serialize.args.hasArg(ReflectionConstants.ATTRIBUTE_IGNORE))
					this._ignore = serialize.args.getArg(ReflectionConstants.ATTRIBUTE_IGNORE).value === "true";
			}
			
			_returnType = xml.@returnType;
			
			_parameters = new Parameters(xml, paramNames);
			
			return true;
		}
		
		private function getParamNames(data:IMetadata) : Array
		{
			var paramNames:Array;
			var arg:IArg = data.args.getArg(ReflectionConstants.PARAMS_NAME);
			if (arg)
			{
				paramNames = arg.value.split(",");
			}
			
			return paramNames;
		}
		/*
	<method name="setMultiValue" declaredBy="nl.haemers.test.data.vo::AddressVO" returnType="void">
    <parameter index="1" type="String" optional="false"/>
    <parameter index="2" type="String" optional="false"/>
    <parameter index="3" type="int" optional="false"/>
    <parameter index="4" type="Object" optional="true"/>
    <metadata name="__go_to_definition_help">
      <arg key="file" value="D:\work\asjlib\trunk\src\nl\haemers\test\data\vo\AddressVO.as"/>
      <arg key="pos" value="823"/>
    </metadata>
  </method>

	<method name="setCountry" declaredBy="nl.haemers.test.data.vo::AddressVO" returnType="void">
    <parameter index="1" type="String" optional="false"/>
    <metadata name="Serialize">
      <arg key="name" value="country"/>
    </metadata>
    <metadata name="__go_to_definition_help">
      <arg key="file" value="D:\work\asjlib\trunk\src\nl\haemers\test\data\vo\AddressVO.as"/>
      <arg key="pos" value="660"/>
    </metadata>
  </method>
  <method name="getCountry" declaredBy="nl.haemers.test.data.vo::AddressVO" returnType="String">
    <metadata name="__go_to_definition_help">
      <arg key="file" value="D:\work\asjlib\trunk\src\nl\haemers\test\data\vo\AddressVO.as"/>
      <arg key="pos" value="747"/>
    </metadata>
  </method>
  
  
  <method name="setCountry" declaredBy="haemers.data.vo::AddressVO" returnType="void">
    <parameter index="1" type="String" optional="false"/>
    <metadata name="Serialize">
      <arg key="params" value="country"/>
    </metadata>
    <metadata name="__go_to_definition_help">
      <arg key="pos" value="836"/>
    </metadata>
  </method>
  <method name="getCountry" declaredBy="haemers.data.vo::AddressVO" returnType="String">
    <metadata name="__go_to_definition_help">
      <arg key="pos" value="923"/>
    </metadata>
  </method>
  <method name="setMultiValue" declaredBy="haemers.data.vo::AddressVO" returnType="void">
    <parameter index="1" type="String" optional="false"/>
    <parameter index="2" type="String" optional="false"/>
    <parameter index="3" type="int" optional="false"/>
    <parameter index="4" type="int" optional="true"/>
    <parameter index="5" type="Object" optional="true"/>
    <metadata name="Serialize">
      <arg key="params" value="value1,value2,value3,value4,value"/>
    </metadata>
    <metadata name="__go_to_definition_help">
      <arg key="pos" value="1054"/>
    </metadata>
  </method>
  <method name="setZipcode" declaredBy="haemers.data.vo::AddressVO" returnType="void">
    <parameter index="1" type="String" optional="false"/>
    <metadata name="Serialize">
      <arg key="params" value="zipcode"/>
    </metadata>
    <metadata name="__go_to_definition_help">
      <arg key="pos" value="538"/>
    </metadata>
  </method>
 

  */
		/** Protected methods **/
		
		/** Private methods **/
		
	}

}